home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Macintosh Sample Code / SC.014.CPlusTESample / Document.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-30  |  7.8 KB  |  265 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0
  4.     File:        Document.h
  5.     Uses:       Application.h
  6.                 List.h
  7.  
  8.     by Andrew Shebanow
  9.     of Apple Macintosh Developer Technical Support
  10.  
  11.     Copyright © 1989-1990 Apple Computer, Inc.
  12.     All rights reserved.
  13.  
  14. ------------------------------------------------------------------------------------------*/
  15.  
  16.  
  17. #ifndef __DOCUMENT__
  18. #define __DOCUMENT__
  19.  
  20. // Include necessary interface files
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24. #ifndef __QUICKDRAW__
  25. #include <QuickDraw.h>
  26. #endif
  27. #ifndef __WINDOWS__
  28. #include <Windows.h>
  29. #endif
  30. #ifndef __FILES__
  31. #include <Files.h>
  32. #endif
  33.  
  34. #ifdef MPW32
  35.  
  36. #ifndef __ALIASES__
  37. #include <Aliases.h>
  38. #endif
  39.  
  40. #else
  41.  
  42. /* canonical file specification */
  43. struct CanonicalFileSpec
  44. {
  45.     short        vRefNum;    /* volume reference number */
  46.     long        dirID;        /* directory ID */
  47.     Str63        fileName;    /* file name */
  48. };
  49.  
  50. #endif
  51.  
  52. #ifndef __APPLICATION__
  53. #include "Application.h"
  54. #endif
  55. #ifndef __LIST__
  56. #include "List.h"
  57. #endif
  58.  
  59. // Define HiWord and LoWord inlines for efficiency.
  60. inline unsigned short HiWord(unsigned long val)
  61. {
  62.     return (unsigned short) ((val >> 16) & 0x0000FFFF);
  63. }
  64.  
  65. inline unsigned short LoWord(unsigned long val)
  66. {
  67.     return (unsigned short) (val & 0x0000FFFF);
  68. }
  69.  
  70. // max and min inlines
  71. inline max(long a, long b)
  72. {
  73.     return a > b ? a : b;
  74. }
  75.  
  76. inline min(long a, long b)
  77. {
  78.     return a < b ? a : b;
  79. }
  80.  
  81. // Define inlines to convert between Points & Rects for convenience.
  82. inline Point TopLeft(Rect r)
  83. {
  84.     Point pt;
  85.  
  86.     pt.v = r.top;
  87.     pt.h = r.left;
  88.     return pt;
  89. }
  90.  
  91. inline Point TopRight(Rect r)
  92. {
  93.     Point pt;
  94.  
  95.     pt.v = r.top;
  96.     pt.h = r.right;
  97.     return pt;
  98. }
  99.  
  100. inline Point BotLeft(Rect r)
  101. {
  102.     Point pt;
  103.  
  104.     pt.v = r.bottom;
  105.     pt.h = r.left;
  106.     return pt;
  107. }
  108.  
  109. inline Point BotRight(Rect r)
  110. {
  111.     Point pt;
  112.  
  113.     pt.v = r.bottom;
  114.     pt.h = r.right;
  115.     return pt;
  116. }
  117.  
  118. const long kMaxSleepTime = 60;    // 1 second worth of ticks
  119.  
  120. // we derive from handle object to prevent fragmentation
  121. class TDocument : public HandleObject {
  122. protected:
  123.     WindowPtr            fDocWindow;
  124.     CanonicalFileSpec    fFile;
  125.     short                fFileRefNum;
  126.     OSType                fFileType;
  127.     Boolean                fNewDoc;
  128.     Boolean                fReadOnly;
  129.     Boolean                fDirty;
  130.     Boolean                fActive;
  131.  
  132.     // internal file i/o routines - these are the guys that do the
  133.     // actual work of manipulating files
  134.     virtual void        OpenFile(Boolean readOnly, Boolean createIfNecessary);
  135.         // OpenFile opens the file referred to by fFile. If createIfNecessary
  136.         // is true and the file doesn't exist, it will be created.
  137.         // If readOnly is true, the fReadOnly field will be set correctly
  138.         // and the file will be opened with a read-only access path.
  139.         // You shouldn't need to override this routine.
  140.     virtual void        CloseFile();
  141.         // close file just closes the file specified by fFileRefNum
  142.         // You shouldn't need to override this routine.
  143.     virtual void        ReadFromFile(short refNum) = 0;
  144.         // this routine reads the file's data from the data fork
  145.         // specified by refNum. The file will already be open,
  146.         // and in the correct position for reading.
  147.         // You MUST override this routine.
  148.     virtual void        WriteToFile(short refNum) = 0;
  149.         // this routine writes the files data to the data fork
  150.         // specified by refNum. The file will already be open,
  151.         // and in the correct position for writing.
  152.         // You MUST override this routine.
  153.  
  154.     virtual YNCResult    PresentSaveDialog(Boolean quitting);
  155.         // this routine puts up the standard Save dialog,
  156.         // using the filename specified by fFile. The quitting
  157.         // parameter is used to make sure we have the right
  158.         // phrasing in the dialog box - either
  159.         // 'Save "x" before quitting?' or 'Save "x" before closing?'
  160.  
  161. public:
  162.     // Constructor & Destructor
  163.                         TDocument(short resID, OSType theFileType);
  164.         // creates new, untitled document using resID as window template
  165.     virtual                ~TDocument();
  166.         // our destructor - disposes of window
  167.  
  168.     // Routines to handle basic user interface events
  169.     // you MUST override these in your subclasses!!!
  170.     virtual void        DoZoom(short partCode) = 0;
  171.     virtual void        DoGrow(EventRecord* theEvent) = 0;
  172.     virtual void        DoContent(EventRecord* theEvent) = 0;
  173.     virtual void        DoKeyDown(EventRecord* theEvent) = 0;
  174.     virtual void        DoUpdate() = 0;
  175.  
  176.     // called when activating/deactivating document.
  177.     // default version just sets fActive variable, so
  178.     // you will need to override this to hilite your selection
  179.     // and such
  180.     virtual void        DoActivate(Boolean becomingActive);
  181.  
  182.     // these routines are called after a TDocument object has been
  183.     // created to initialize its contents. You probably won't need to
  184.     // override these, since they are fairly generic.
  185.     virtual void        OpenNewDoc() {};
  186.         // OpenNewDoc sets up a new, untitled document - by default, this
  187.         // has already been done in the TDocument constructor, so this routine
  188.         // is just a placeholder.
  189.     virtual void        OpenOldDoc(CanonicalFileSpec theFile, Boolean readOnly);
  190.         // OpenOldDoc opens a document, reads its contents, sets the window
  191.         // title, and so on.
  192.  
  193.     // high level, user-oriented file handling routines
  194.     // You probably won't need to override these
  195.     virtual YNCResult    DoClose(Boolean askUserToSave,
  196.                                 YNCResult defaultAnswer,
  197.                                 Boolean quitting);
  198.         // DoClose closes the document. If askUserToSave is true,
  199.         // the user is asked whether or not he wants to save the documents
  200.         // contents. If it is false, the document will be saved if defaultAnswer
  201.         // is yesResult.
  202.     virtual void        DoSave();
  203.         // DoSave saves the documents contents. If the document is a new,
  204.         // untitled document, DoSave will call DoSaveAs (below). Otherwise,
  205.         // it sets up the file for writing and calls WriteToFile to save the
  206.         // actual data.
  207.     virtual void        DoSaveAs();
  208.         // DoSaveAs asks the user for a file to save the document into.
  209.         // It saves the data in the same manner as DoSave, and sets up
  210.         // the file-related data members (fFile, fFileRefNum, etc).
  211.     virtual void        DoRevert() {};
  212.         // DoRevert is a hook for a revert routine. Currently, this
  213.         // isn't implemented, but it is here for future enhancement.
  214.     virtual void        DoPrint() {};
  215.         // we don't support printing yet either
  216.  
  217.     // standard edit menu actions
  218.     // with the exception of DoUndo, you MUST override these routines
  219.     virtual void        DoUndo() {};
  220.         // by default, undo is unimplemented, so you don't have to
  221.         // override this method
  222.     virtual void        DoCut() = 0;
  223.     virtual void        DoCopy() = 0;
  224.     virtual void        DoPaste() = 0;
  225.     virtual void        DoClear() = 0;
  226.     virtual void        DoSelectAll() = 0;
  227.  
  228.     // idle time routines: you can override these to do cursor handling,
  229.     // TE caret blinking, marquee effects, etc...
  230.     virtual void        DoIdle() {};
  231.     virtual unsigned long CalcIdle() { return kMaxSleepTime; };
  232.         // we never need idle in typical applications, so return a very large number
  233.     virtual void        AdjustCursor(Point where) {};
  234.         // where is in local coords
  235.  
  236.     // query state of document - useful for adjusting menu state
  237.     // You will probably need to override at least a few of
  238.     // these to accurately reflect the state of your document
  239.     virtual Boolean        HaveUndo() { return false; };
  240.     virtual Boolean        HaveSelection() { return false; };
  241.     virtual Boolean        HavePaste() { return false; };
  242.     virtual Boolean        CanClose() { return (FrontWindow() != nil); };
  243.     virtual Boolean        CanSave() { return fDirty; };
  244.     virtual Boolean        CanSaveAs() { return true; };
  245.     virtual Boolean        CanRevert() { return false; };    // not implemented
  246.     virtual Boolean        CanPrint() { return false; };    // not implemented
  247.  
  248.     // utility routine to get window pointer for document
  249.     inline WindowPtr    GetDocWindow() { return fDocWindow; }
  250. };
  251.  
  252. // TDocumentList is a simple linked list of documents,
  253. // implemented C++ style.
  254. class TDocumentList : public TList {
  255. public:
  256.     virtual void        AddDoc(TDocument* doc);
  257.     virtual void        RemoveDoc(TDocument* doc);
  258.     // find the TDocument associated with the window
  259.     virtual TDocument*    FindDoc(WindowPtr window);
  260.     // return number of active documents
  261.     inline int            NumDocs() { return Count(); }
  262. };
  263.  
  264. #endif
  265.